package com.cloudwise.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cloudwise.entity.User;
import com.cloudwise.service.MenuService;
import com.cloudwise.service.QuestionService;
import com.cloudwise.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * @author:高杰领
 * @date:2022/1/20 9:52
 * @description:提供页面的访问请求处理,所有请求请返回modelAndView
 * @version:1.0
 **/
@RestController
public class ForwardController {
    /**
     * 所有请求请返回modelAndView
     * **/
    @Resource
    QuestionService questionService;
    @Resource
    UserService userService;
    @Resource
    MenuService menuService;

    /**
     *
     * @param
     * @return java.lang.String
     * @author 高冷
     * @description:请求此地址访问管理员登录界面
     * @date 2021/11/26 16:37
     */
    @RequestMapping("/showAdminLogin")
    public ModelAndView showAdminLogin(ModelAndView modelAndView){
        modelAndView.setViewName("index");
        return modelAndView;
    }
    @RequestMapping("/showDept")
    public ModelAndView showDept(ModelAndView modelAndView){
        modelAndView.setViewName("dept");
        return modelAndView;
    }
    /**
     *
     * @param
     * @return java.lang.String
     * @author 高冷
     * @description:显示大布局页面(不用输入任何路径也可以直接访问)
     * @date 2021/12/3 9:19
     */
    @RequestMapping("/showIndex")
    public ModelAndView showIndex(ModelAndView modelAndView,String loginName){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("login_name",loginName);
        User user = userService.getOne(queryWrapper);
        List<Tree<String>> allMenuByLoginName = menuService.getAllMenuByLoginName(loginName);
        Set<String> allPermsByLoginName = menuService.getAllPermsByLoginName(loginName);
        modelAndView.addObject("user",user);
        modelAndView.addObject("menu",allMenuByLoginName);
        modelAndView.addObject("perms",allPermsByLoginName);
        modelAndView.setViewName("home");
        return modelAndView;
    }
    /**
     *
     * @param
     * @return java.lang.String
     * @author 高冷
     * @description:访问菜单管理页面
     * @date 2021/12/3 9:20
     */
    @RequestMapping("/showMenu")
    public ModelAndView showMenu(ModelAndView modelAndView){
        modelAndView.setViewName("menu");
        return modelAndView;
    }
    @RequestMapping("/showRole")
    public ModelAndView showRole(ModelAndView modelAndView){
        modelAndView.setViewName("role");
        return modelAndView;
    }
    @RequestMapping("/showResultOne")
    public ModelAndView showResultOne(ModelAndView modelAndView,String id){
        modelAndView.addObject("id",id);
        modelAndView.setViewName("resultOne");
        return modelAndView;
    }
    /** *
     * @Author 张燕豪
     * @Description
     * @data 19:09 2022/1/22
     * @return 学生注册
     */
    @RequestMapping("/showStudent")
    public ModelAndView showStudent(ModelAndView modelAndView){

        modelAndView.setViewName("StudentEnroll");
        return modelAndView;
    }

    @RequestMapping("/showUser")
    public ModelAndView showUser(ModelAndView modelAndView){
        modelAndView.setViewName("user");
        return modelAndView;
    }
    @RequestMapping("/showLog")
    public ModelAndView showLog(ModelAndView modelAndView){
        modelAndView.setViewName("log");
        return modelAndView;
    }
    @RequestMapping("/showUserInfo")
    public ModelAndView showUserInfo(ModelAndView modelAndView){
        Subject subject = SecurityUtils.getSubject();
        Object principal = subject.getPrincipal();
        User user=new User();
        if(principal!=null){
            user= (User) principal;
        }
        User byId = userService.getById(user.getId());
        modelAndView.setViewName("userInfo");
        modelAndView.addObject("user",byId);
        return modelAndView;
    }
    @RequestMapping("/showBusiness")
    public ModelAndView showBusiness(ModelAndView modelAndView){
        modelAndView.setViewName("business");
        return modelAndView;
    }
    @RequestMapping("/showSpeciality")
    public ModelAndView showSpeciality(ModelAndView modelAndView){
        modelAndView.setViewName("speciality");
        return modelAndView;
    }
    @RequestMapping("/showEducationType")
    public ModelAndView showEducationType(ModelAndView modelAndView){
        modelAndView.setViewName("educationtype");
        return modelAndView;
    }

}
